﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using ChoDiaOc_HK_WPF.Controls;

namespace ChoDiaOc_HK_WPF.Views.QuanLy {
    /// <summary>
    /// Interaction logic for QuanKyDangNhap.xaml
    /// </summary>
    public partial class QuanLyDangNhap : UserControl {
        public QuanLyDangNhap() {
            InitializeComponent();
            entities = new DBEntities();
            //List<String> list = new List<String>() { "Admin", "Saler", "Photographer", "Reporter", "Customer" };            
            //quyenComboBox.ItemsSource = list;
        }
        private DBEntities entities;
        private void UserControl_Loaded(object sender, RoutedEventArgs e) {
            System.Windows.Data.CollectionViewSource myCollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["dANGNHAPsViewSource"];            
            myCollectionViewSource.Source = entities.DANGNHAPs;
        }

        private void btnHoanTat_Click(object sender, RoutedEventArgs e) {
            try {
                entities.SaveChanges();             
                MessageBox.Show("Cập nhật thành công!", "Thông báo", MessageBoxButton.OK, MessageBoxImage.Information);
            } catch (Exception exp) {
                MessageBox.Show(exp.Message);
            }
        }

        private void btnNhaplai_Click(object sender, RoutedEventArgs e) {
            System.Windows.Data.CollectionViewSource myCollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["dANGNHAPsViewSource"];
            entities = new DBEntities();
            myCollectionViewSource.Source = entities.DANGNHAPs;
        }

        private void btnThem_Click(object sender, RoutedEventArgs e) {
            if (dANGNHAPsListView.SelectedIndex != -1) {
                dANGNHAPsListView.SelectedIndex = -1;
                System.Windows.Data.CollectionViewSource myCollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["kHACHHANGsViewSource"];
                myCollectionViewSource.Source = null;
                matKhauTextBox.IsReadOnly = false;
                quyenComboBox.IsReadOnly = false;
            } else {
                if (MessageBoxResult.Yes == MessageBox.Show("Chắc chắn?", "Cảnh báo", MessageBoxButton.YesNo, MessageBoxImage.Question)) {

                    DANGNHAP dn = new DANGNHAP();
                    dn.MaDangNhap = TiepNhanDangKyController.GetInstance().GenerateMaDANGNHAP();
                    dn.TenDangNhap = tenDangNhapTextBox.Text;
                    dn.MatKhau = matKhauTextBox.Text;
                    dn.Quyen = quyenComboBox.Text;

                    entities.AddToDANGNHAPs(dn);
                    entities.SaveChanges();
                    System.Windows.Data.CollectionViewSource myCollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["dANGNHAPsViewSource"];
                    entities = new DBEntities();
                    myCollectionViewSource.Source = entities.DANGNHAPs;
                }
            }
        }

        private void btnXoa_Click(object sender, RoutedEventArgs e) {
            if (dANGNHAPsListView.SelectedIndex != -1) {
                DANGNHAP dn = dANGNHAPsListView.SelectedItem as DANGNHAP;
                if (dn.Quyen == "Customer") {
                    MessageBox.Show("Không thể xoá khách hàng!", "Thông báo", MessageBoxButton.OK, MessageBoxImage.Information);
                    return;
                }
                if (MessageBoxResult.Yes == MessageBox.Show("Chắc chắn?", "Cảnh báo", MessageBoxButton.YesNo, MessageBoxImage.Question)) {
                    entities.DANGNHAPs.DeleteObject(dANGNHAPsListView.SelectedItem as DANGNHAP);
                    entities.SaveChanges();
                }
            }
        }

        private void dANGNHAPsListView_SelectionChanged(object sender, SelectionChangedEventArgs e) {
            if (dANGNHAPsListView.SelectedIndex != -1) {
                DANGNHAP dn = dANGNHAPsListView.SelectedItem as DANGNHAP;
                if (dn.Quyen == "Customer") {
                    matKhauTextBox.IsReadOnly = true;
                    quyenComboBox.IsReadOnly = true;
                    System.Windows.Data.CollectionViewSource myCollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["kHACHHANGsViewSource"];
                    myCollectionViewSource.Source = entities.KHACHHANGs.Where(kh => kh.MaKH == dn.MatKhau);
                } else {
                    System.Windows.Data.CollectionViewSource myCollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["kHACHHANGsViewSource"];
                    myCollectionViewSource.Source = null;
                    matKhauTextBox.IsReadOnly = false;
                    quyenComboBox.IsReadOnly = false;
                }
            }
        }
    }
}
